<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//session functions
define('RT_STORE_SESSIONS', 'mysql');

//
if( RT_STORE_SESSIONS == 'mysql' ) {
	if( get_cfg_var('session.gc_maxlifetime') ) {
		define('RT_SESSION_LIFE_TIME', get_cfg_var('session.gc_maxlifetime'));
	} else {
		//60 * 60 * 24 * 30, 30 days
		//60 * 60 * 24 * 1, 1 days = 86400
		define('RT_SESSION_LIFE_TIME', 86400);
	}
	
	//
	function _sess_open($save_path, $session_name) {
		return true;
	}
	
	function _sess_close() {
		return true;
	}
	
	function _sess_read($key) {
		global $adb;
		
		$query = "select value from pp_sessions where sesskey = '" . func_db_input($key) . "' and expiry > '" . time() . "'";
		$result = $adb->query($query);
		$row = $adb->fetch_array($result);
		if( $row['value'] ) {
			return $row['value'];
		}
		return false;
	}
	
	function _sess_write($key, $val) {
		global $adb;
		
		$expiry = time() + RT_SESSION_LIFE_TIME;
		$value = $val;
		
		$query = "select count(*) as total from pp_sessions where sesskey = '" . func_db_input($key) . "'";
		$result = $adb->query($query);
		$row = $adb->fetch_array($result);
		
		if( $row['total'] > 0) {
			$adb->query("update pp_sessions set expiry = '" . func_db_input($expiry) . "', value = '" . func_db_input($value) . "' where sesskey = '" . func_db_input($key) . "'");
			return true;
		} else {
			$adb->query("insert into pp_sessions values ('" . func_db_input($key) . "', '" . func_db_input($expiry) . "', '" . func_db_input($value) . "')");
			return true;
		}
	}
	
	function _sess_destroy($key) {
		global $adb;
		
		$adb->query("delete from pp_sessions where sesskey = '" . func_db_input($key) . "'");
		return true;
	}
	
	function _sess_gc($maxlifetime) {
		global $adb;
		
		$adb->query("delete from pp_sessions where expiry < '" . time() . "'");
		
		return true;
	}

	session_set_save_handler('_sess_open', '_sess_close', '_sess_read', '_sess_write', '_sess_destroy', '_sess_gc');
}

//
function func_session_start() {
	return session_start();
}

function func_session_register($variable) {
	global $session_started;
	
	if( $session_started == true ) {
		return session_register($variable);
	} else {
		return false;
	}
}

function func_session_is_registered($variable) {
	return session_is_registered($variable);
}

function func_session_unregister($variable) {
	return session_unregister($variable);
}

function func_session_id($sessid = '') {
	if( !empty($sessid) ) {
		return session_id($sessid);
	} else {
		return session_id();
	}
}

function func_session_name($name = '') {
	if( !empty($name) ) {
		return session_name($name);
	} else {
		return session_name();
	}
}

function func_session_close() {
	if( PHP_VERSION >= '4.0.4' ) {
		return session_write_close();
	} elseif( function_exists('session_close') ) {
		return session_close();
	}
}

function func_session_destroy() {
	return session_destroy();
}

function func_session_save_path($path = '') {
	if( !empty($path) ) {
		return session_save_path($path);
	} else {
		return session_save_path();
	}
}

function func_session_recreate() {
	if( PHP_VERSION >= 4.1 ) {
		$session_backup = $_SESSION;
		
		unset($_COOKIE[func_session_name()]);
		
		func_session_destroy();
		
		if( RT_STORE_SESSIONS == 'mysql' ) {
			session_set_save_handler('_sess_open', '_sess_close', '_sess_read', '_sess_write', '_sess_destroy', '_sess_gc');
		}
		
		func_session_start();
		
		$_SESSION = $session_backup;
		unset($session_backup);
	}
}
//
?>